Musiker.modSearch = {
	name			: 	"SearchLibrary",
	currentSearch	: 	null,
	searchData		: 	{}
}

Musiker.modSearch.Initialize = function () {
	var aFrame = document.createElement('div');
		aFrame.className = 'cssPlaylistContainerFrame';
		aFrame.show = MUSIKER_SHOWPANEL;
		aFrame.hide = MUSIKER_HIDEPANEL;
		aFrame.hide();
		Musiker.modSearch.Panel = aFrame
	Musiker.frames.DisplayPanel.appendChild(aFrame);
	
	//wrapping div
	var cFrame = document.createElement('div');
		cFrame.className = 'scrollingWrapper';
		cFrame.style.top = '17px';
		cFrame.style.bottom = '33px';
	Musiker.modSearch.Panel.appendChild(cFrame);	

	//contentTable
	var aFrame = document.createElement('table');
		aFrame.className = 'cssPlaylist';
		
		Musiker.modSearch.listPanel = aFrame;
	cFrame.appendChild(aFrame);
	
		var aFrame = document.createElement('div');
			aFrame.className = 'cssFillerFrame';
		Musiker.modSearch.listPanel.appendChild(aFrame);
		

	//header
	var aFrame = document.createElement('table');
		aFrame.className = 'cssPlaylistHeader';
		aFrame.innerHTML = "<tr class='cssPlaylistItem' header='yes'><td class='cssPlaylistItem_Num'>&nbsp;</td><td class='cssPlaylistItem_Title'>Title</td><td class='cssPlaylistItem_Artist'>Artist</td><td class='cssPlaylistItem_Album'>Album</td><td class='cssPlaylistItem_Time'>Time</td><td class='cssPlaylistItem_Rating'>Rating</td><td></td></tr>";
	Musiker.modSearch.Panel.appendChild(aFrame);


	//action bar
	var aFrame = document.createElement('div');
		aFrame.className = 'cssActionBar';
		Musiker.modSearch.actionBarPanel = aFrame;
	Musiker.modSearch.Panel.appendChild(aFrame);

		var aFrame = document.createElement('div');
			aFrame.className = 'cssActionBarProgressFrame';
			aFrame.show = MUSIKER_SHOWPANEL;
			aFrame.hide = MUSIKER_HIDEPANEL;
			aFrame.set = function (percent) {
				this.fill.style.width = Math.round(percent)+'%';
			}
			Musiker.modSearch.actionBarPanel.progressBar = aFrame;
		Musiker.modSearch.actionBarPanel.appendChild(aFrame);
		
			var bFrame = document.createElement('div');
				bFrame.className = 'cssActionBarProgressFill';
				Musiker.modSearch.actionBarPanel.progressBar.fill = bFrame;
			aFrame.appendChild(bFrame);
			
			
		var aFrame = document.createElement('input'); //setup search field
			aFrame.setAttribute('type','search');
			aFrame.className = 'cssActionBarSearch';
			aFrame.lastValue = "";
			aFrame.onchange = function () {
				if (this.value != this.lastValue) {
					Musiker.modSearch.search(this.value);
					this.lastValue = this.value;
				}
			} 
			aFrame.onsearch = function () {
				if (this.value=="" && this.lastValue!="") {
					//insert clearing code here, if you feel the need later
					this.lastValue = "";
				}
			}
			aFrame.onkeydown = function (e) { //this is here so that the spacebar and arrow keys still work
				if (!e && window.event) window.event.cancelBubble(true);
				else e.stopPropagation(true);
				return true;
			}
			Musiker.modSearch.actionBarPanel.searchField = aFrame;
		Musiker.modSearch.actionBarPanel.appendChild(aFrame);

	//	Setup Sidebar Row --------------------------------------------------------------------------------------
	var aFrame = document.createElement('div');
		aFrame.className = 'cssSidebar_Row';
		aFrame.innerHTML = "<img src='images/iTunesMusic.png'><a>Search Library</a>";
		aFrame.onclick = function () {
			Musiker.selectModule(Musiker.modSearch);
		}
		Musiker.modSearch.sidebarItem = aFrame;
	Musiker.frames.SidebarPanel.appendChild(aFrame);
}

Musiker.modSearch.show = function () {
	Musiker.modSearch.Panel.show();
	Musiker.Sidebar.setSelection(Musiker.modSearch.sidebarItem);
}
Musiker.modSearch.hide = function () {
	Musiker.modSearch.Panel.hide();
}

Musiker.modSearch.search = function (query, calledFromData) {
	if (!calledFromData) Musiker.modSearch.currentSearch = query;
	else if (Musiker.modSearch.currentSearch != query) return;
	
	//clear the playlist frame of all contents
	while (Musiker.modSearch.listPanel.childNodes.length > 0) {
		Musiker.modSearch.listPanel.removeChild( Musiker.modSearch.listPanel.firstChild );       
	}
	Musiker.modSearch.listPanel.parentNode.scrollTop = 0;
	
	if (Musiker.modSearch.searchData[query]) {  //playlist content is loaded, building the frame	
		
		Musiker.modSearch.actionBarPanel.progressBar.show();
		Musiker.modSearch.actionBarPanel.progressBar.set(0);
		Musiker.modSearch._bufferedContentDrawing(query, 0, 100)

	} else {	//no playlist content loaded, fetch it
		var aFrame = document.createElement('div');
			aFrame.className = 'cssWaitFrame';
		Musiker.modSearch.listPanel.appendChild(aFrame);
		
		SimpleFetch.FetchAndEval('FetchSearchContent.js.php?q='+encodeURI(query));		
	}
}

Musiker.modSearch._bufferedContentDrawing = function (query, startValue, stopCount) {
	//abort if the user has switched playlists
	if (Musiker.modSearch.currentSearch != query) return;
	
	var playlistData = Musiker.modSearch.searchData[query];

	//row items
	var oddfield = 0;
	var c = Math.min(startValue+stopCount,playlistData.length); //only draw up to what the function is allowed
	for (var i=startValue;i<c;i++) {
		var o = playlistData[i];
		oddfield = ++oddfield % 2;
	
		var aFrame = Musiker.buildRow(o,null,oddfield);
			aFrame.playIndex = i;
			aFrame.songObject = o;
			aFrame.ondblclick = function () {
				MediaPlayer.appendAndPlay(
					Base64.decode(this.songObject.filepath).replace('\+','\%2B'), //the replacement is because mediaplayer thinks + is a space and requests the wrong file
					this.songObject.title,
					this.songObject.artist,
					this.songObject.filepath,
					this.songObject.album
				);
				return false;
			}
		Musiker.modSearch.listPanel.appendChild(aFrame);
	}
	
	//if there's still playlist data remaining, queue up next batch
	if (i<playlistData.length-1) {
		Musiker.modSearch.actionBarPanel.progressBar.set((i/playlistData.length)*100);
		setTimeout('Musiker.modSearch._bufferedContentDrawing("'+query+'", '+i+',50)',1);
	} else Musiker.modSearch.actionBarPanel.progressBar.hide();
}

Musiker.RegisterModule(Musiker.modSearch);